﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BusinessLayer.WorldArtPrice.Entities;
using DBHelper;

namespace BusinessLayer.WorldArtPrice
{
    public class CurrencyBL
    {
        private readonly DbHelper _db;

        public CurrencyBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public void Insert(Currencys country)
        {
            var oCommand = new SqlCommand("sp_Currency_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Currency", country.Currency));
            oCommand.Parameters.Add(new SqlParameter("@Country", country.Country));
            _db.ExecuteNonQuery(oCommand);
        }

        public void Update(Currencys country)
        {
            var oCommand = new SqlCommand("sp_Currency_Update") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", country.Id));
            oCommand.Parameters.Add(new SqlParameter("@Currency", country.Currency));
            oCommand.Parameters.Add(new SqlParameter("@Country", country.Country));
            _db.ExecuteNonQuery(oCommand);
        }

        public void Delete(int id)
        {
            var oCommand = new SqlCommand("sp_Currency_Delete") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            _db.ExecuteNonQuery(oCommand);
        }

        public List<Currencys> Search(string keyWord, int pageIndex, int pageSize, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_Currency_Select_By_KeyWord") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@KeyWord", keyWord));
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            var list = _db.GetList<Currencys>(oCommand);
            totalRecord = (int)output.Value;
            return list;
        }

        public Currencys GetById(int id)
        {
            var oCommand = new SqlCommand("sp_Currency_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            return _db.GetList<Currencys>(oCommand).FirstOrDefault();                       
        }
    }
}
